<template>
    <div class="app-container">
        <el-form
            :model="queryParams"
            ref="queryForm"
            size="small"
            :inline="true"
            v-show="showSearch"
            label-width="68px"
        >
            <!-- 证书类别 -->
            <el-form-item label="证书类别" prop="certificateType">
                <el-select
                    v-model="queryParams.certificateType"
                    placeholder=""
                    clearable
                >
                    <el-option
                        v-for="dict in dict.type.zs_type"
                        :key="dict.value"
                        :label="dict.label"
                        :value="dict.value"
                    />
                </el-select>
            </el-form-item>

            <el-form-item label="取得时间" prop="daterange">
                <el-date-picker
                    v-model="queryParams.timeArr"
                    type="daterange"
                    range-separator="至"
                    start-placeholder="开始日期"
                    end-placeholder="结束日期"
                    value-format="yyyy-MM-dd"
                >
                </el-date-picker>
            </el-form-item>

            <el-form-item>
                <el-button
                    type="primary"
                    icon="el-icon-search"
                    size="mini"
                    @click="handleQuery"
                    >查询</el-button
                >
                <el-button
                    icon="el-icon-refresh"
                    size="mini"
                    @click="resetQuery"
                    >重置</el-button
                >
            </el-form-item>
        </el-form>

        <TableCard>
            <el-row :gutter="10" class="mb8"> </el-row>

            <el-table
                :border="Global.tableShowBorder"
                v-loading="loading"
                :data="certificateList"
                @selection-change="handleSelectionChange"
            >
                <!-- <el-table-column type="selection" width="55" align="center" />
                <el-table-column label="证书id" align="center" prop="id" /> -->
                <el-table-column label="序号" align="center" type="index" />

                <!-- <el-table-column label="岗位" align="center" prop="postName" /> -->
                <el-table-column
                    label="证书名称"
                    align="center"
                    prop="certificateName"
                />
                <el-table-column label="证书编号" align="center" prop="code" />

                <el-table-column
                    label="证书类别"
                    align="center"
                    prop="certificateTypeName"
                />
                <el-table-column
                    label="证书文件"
                    align="center"
                    prop="certificateUrls"
                >
                    <template slot-scope="scope">
                        <span>{{
                           scope.row.certificateUrls ?  scope.row.certificateUrls.split(",").length : 0
                        }}</span>
                    </template>
                </el-table-column>
                <el-table-column
                    label="取得时间"
                    align="center"
                    prop="getDate"
                    width="180"
                >
                    <template slot-scope="scope">
                        <span>{{
                            parseTime(scope.row.getDate, "{y}-{m}-{d}")
                        }}</span>
                    </template>
                </el-table-column>

                <el-table-column
                    label="操作"
                    align="center"
                    class-name="small-padding fixed-width"
                >
                    <template slot-scope="scope">
                        <el-button
                            size="mini"
                            type="text"
                            icon="el-icon-edit"
                            v-if="scope.row.certificateType == '1'"
                            @click="viewXl(scope.row)"
                            >查看</el-button
                        >
                        <el-button
                            size="mini"
                            type="text"
                            icon="el-icon-edit"
                            v-if="scope.row.certificateType == '2'"
                            @click="viewXw(scope.row)"
                            >查看</el-button
                        >
                    </template>
                </el-table-column>
            </el-table>

            <pagination
                v-show="total > 10"
                :total="total"
                :page.sync="queryParams.pageNum"
                :limit.sync="queryParams.pageSize"
                @pagination="getList"
            />
        </TableCard>

        <!-- 添加或修改劳动合同对话框 -->
        <el-dialog
            :title="title"
            :visible.sync="open"
            width="1200px"
            append-to-body
        >
            <TableCard>
                <el-form
                    ref="form"
                    :model="form"
                    :rules="rules"
                    label-width="120px"
                >
                    <el-row>
                        <el-col :xs="24" :sm="24" :md="24" :lg="24">
                            <el-divider content-position="left"
                                >员工信息</el-divider
                            >
                        </el-col>
                        <el-col :span="12">
                            <el-form-item label="员工" prop="realName">
                                <el-input
                                    suffix-icon="el-icon-search"
                                    readonly
                                    @focus="openSecelctOneUser"
                                    v-model="form.realName"
                                    placeholder="请选择员工"
                                />
                            </el-form-item>
                        </el-col>
                        <el-col :span="12">
                            <el-form-item label="工号" prop="staffNum">
                                <el-input
                                    disabled
                                    v-model="form.staffNum"
                                    placeholder="请输入工号"
                                />
                            </el-form-item>
                        </el-col>

                        <el-col :span="12">
                            <!-- 部门 -->

                            <el-form-item label="部门" prop="deptId">
                                <treeselect
                                    v-model="form.deptId"
                                    :options="deptOptions"
                                    :show-count="true"
                                    placeholder="请选择部门"
                                    disabled
                                />
                            </el-form-item>
                        </el-col>
                        <el-col :span="12">
                            <!-- 岗位 -->
                            <el-form-item label="岗位" prop="postId">
                                <el-select
                                    disabled
                                    v-model="form.postId"
                                    placeholder="请选择岗位"
                                    clearable
                                >
                                    <el-option
                                        v-for="item in postList"
                                        :key="item.postId"
                                        :label="item.postName"
                                        :value="item.postId"
                                    >
                                    </el-option>
                                </el-select>
                            </el-form-item>
                        </el-col>
                    </el-row>
                    <el-row>
                        <el-col :xs="24" :sm="24" :md="24" :lg="24">
                            <el-divider content-position="left"
                                >学位证信息</el-divider
                            >
                        </el-col>

                        <el-col :span="12">
                            <el-form-item
                                label="证书名称"
                                prop="certificateName"
                            >
                                <el-input
                                    v-model="form.certificateName"
                                    placeholder="请输入证书名称"
                                />
                            </el-form-item>
                        </el-col>
                        <el-col :span="12">
                            <el-form-item label="证书编号" prop="code">
                                <el-input
                                    v-model="form.code"
                                    placeholder="请输入证书编号"
                                />
                            </el-form-item>
                        </el-col>

                        <el-col :span="12">
                            <el-form-item
                                label="毕业院校"
                                v-show="form.certificateType == 1"
                                prop="school"
                            >
                                <el-input
                                    v-model="form.school"
                                    placeholder="请输入毕业院校"
                                />
                            </el-form-item>
                        </el-col>
                        <el-col :span="12">
                            <el-form-item
                                v-show="form.certificateType == 1"
                                label="学历层次"
                                prop="education"
                            >
                                <el-select
                                    v-model="form.education"
                                    placeholder="请选择学历层次"
                                />
                            </el-form-item>
                        </el-col>
                        <el-col :span="12">
                            <el-form-item label="专业" prop="major">
                                <el-input
                                    v-model="form.major"
                                    placeholder="请输入专业"
                                />
                            </el-form-item>
                        </el-col>
                        <el-col :span="12">
                            <el-form-item
                                v-show="form.certificateType == 2"
                                label="学位名称"
                                prop="degree"
                            >
                                <el-input
                                    v-model="form.degree"
                                    placeholder="请输入学位名称"
                                />
                            </el-form-item>
                        </el-col>
                        <el-col :span="12">
                            <el-form-item
                                v-show="form.certificateType == 1"
                                label="学制"
                                prop="xuezhi"
                            >
                                <el-select
                                    v-model="form.xuezhi"
                                    placeholder="请选择学制"
                                />
                            </el-form-item>
                        </el-col>
                        <el-col :span="12">
                            <el-form-item
                                v-show="form.certificateType == 1"
                                label="毕业时间"
                                prop="getDate"
                            >
                                <el-date-picker
                                    clearable
                                    v-model="form.graduationDate"
                                    type="date"
                                    value-format="yyyy-MM-dd"
                                    placeholder="请选择毕业时间"
                                >
                                </el-date-picker>
                            </el-form-item>
                        </el-col>

                        <el-col :span="12" v-show="form.certificateType == 2">
                            <el-form-item label="授予单位" prop="fromDw">
                                <el-input
                                    v-model="form.fromDw"
                                    placeholder="请输入授予单位"
                                /> </el-form-item
                        ></el-col>
                        <el-col :span="12">
                            <el-form-item label="取得时间" prop="getDate">
                                <el-date-picker
                                    clearable
                                    v-model="form.getDate"
                                    type="date"
                                    value-format="yyyy-MM-dd"
                                    placeholder="请选择取得时间"
                                >
                                </el-date-picker>
                            </el-form-item>
                        </el-col>
                        <el-col :span="24">
                            <el-form-item label="描述" prop="desc">
                                <el-input
                                    v-model="form.desc"
                                    type="textarea"
                                    :rows="5"
                                    placeholder="请输入描述"
                                />
                            </el-form-item>
                        </el-col>
                        <el-col :span="24">
                            <el-form-item label="证书" prop="certificateUrls">
                                <file-upload
                                    v-model="form.certificateUrls"
                                    :fileSize="10"
                                    :fileType="['jpg', 'png', 'jpeg']"
                                    :limit="5"
                                />
                            </el-form-item>
                        </el-col>
                    </el-row>

                    <!-- <el-form-item label="员工姓名" prop="realName">
                        <el-input
                            v-model="form.realName"
                            placeholder="请输入员工姓名"
                        />
                    </el-form-item>

                    <el-form-item label="员工id" prop="userId">
                        <el-input
                            v-model="form.userId"
                            placeholder="请输入员工id"
                        />
                    </el-form-item>
                    <el-form-item label="所属部门id" prop="deptId">
                        <el-input
                            v-model="form.deptId"
                            placeholder="请输入所属部门id"
                        />
                    </el-form-item>
                    <el-form-item label="所属部门" prop="deptName">
                        <el-input
                            v-model="form.deptName"
                            placeholder="请输入所属部门"
                        />
                    </el-form-item>
                    <el-form-item label="岗位ID" prop="postId">
                        <el-input
                            v-model="form.postId"
                            placeholder="请输入岗位ID"
                        />
                    </el-form-item>
                    <el-form-item label="岗位名称" prop="postName">
                        <el-input
                            v-model="form.postName"
                            placeholder="请输入岗位名称"
                        />
                    </el-form-item>
                    <el-form-item label="证书名称" prop="certificateName">
                        <el-input
                            v-model="form.certificateName"
                            placeholder="请输入证书名称"
                        />
                    </el-form-item>
                    <el-form-item label="证书url列表" prop="certificateUrls">
                        <el-input
                            v-model="form.certificateUrls"
                            type="textarea"
                            :rows="5"
                            placeholder="请输入内容"
                        />
                    </el-form-item>

                    <el-form-item label="备注" prop="remark">
                        <el-input
                            v-model="form.remark"
                            placeholder="请输入备注"
                        />
                    </el-form-item>
                    <el-form-item label="证书编号" prop="certificateCode">
                        <el-input
                            v-model="form.certificateCode"
                            placeholder="请输入证书编号"
                        />
                    </el-form-item> -->
                </el-form>
            </TableCard>
            <div slot="footer" class="dialog-footer">
                <el-button type="primary" @click="submitForm">保 存</el-button>
                <el-button @click="cancel">取 消</el-button>
            </div>
        </el-dialog>

        <!-- 查看学历证 -->
        <el-dialog
            :title="title"
            :visible.sync="showXl"
            width="1200px"
            append-to-body
        >
            <TableCard>
                <el-form
                    ref="form"
                    :model="form"
                    :rules="rules"
                    label-width="120px"
                >
                    <el-row>
                        <el-col :xs="24" :sm="24" :md="24" :lg="24">
                            <el-divider content-position="left"
                                >员工信息</el-divider
                            >
                        </el-col>
                        <el-col :span="12">
                            <el-form-item label="姓名">
                                <el-input disabled v-model="form.realName" />
                            </el-form-item>
                        </el-col>
                        <el-col :span="12">
                            <el-form-item label="工号">
                                <el-input disabled v-model="form.staffNum" />
                            </el-form-item>
                        </el-col>

                        <el-col :span="12">
                            <!-- 部门 -->

                            <el-form-item label="部门">
                                <treeselect
                                    v-model="form.deptId"
                                    :options="deptOptions"
                                    :show-count="true"
                                    disabled
                                />
                            </el-form-item>
                        </el-col>
                        <el-col :span="12">
                            <!-- 岗位 -->
                            <el-form-item label="岗位">
                                <el-select disabled v-model="form.postId">
                                    <el-option
                                        v-for="item in postList"
                                        :key="item.postId"
                                        :label="item.postName"
                                        :value="item.postId"
                                    >
                                    </el-option>
                                </el-select>
                            </el-form-item>
                        </el-col>
                    </el-row>
                    <el-row>
                        <el-col :xs="24" :sm="24" :md="24" :lg="24">
                            <el-divider content-position="left"
                                >学历证信息</el-divider
                            >
                        </el-col>

                        <el-col :span="12">
                            <el-form-item label="证书名称">
                                <el-input
                                    disabled
                                    v-model="form.certificateName"
                                />
                            </el-form-item>
                        </el-col>
                        <el-col :span="12">
                            <el-form-item label="证书编号">
                                <el-input v-model="form.code" disabled />
                            </el-form-item>
                        </el-col>

                        <el-col :span="12">
                            <el-form-item label="毕业院校" prop="school">
                                <el-input disabled v-model="form.school" />
                            </el-form-item>
                        </el-col>
                        <el-col :span="12">
                            <el-form-item label="学历层次" prop="education">
                                <el-select
                                    disabled
                                    v-model="form.education"
                                    placeholder="请选择学历层次"
                                />
                            </el-form-item>
                        </el-col>
                        <el-col :span="12">
                            <el-form-item label="专业" prop="major">
                                <el-input disabled v-model="form.major" />
                            </el-form-item>
                        </el-col>

                        <el-col :span="12">
                            <el-form-item label="学制" prop="xuezhi">
                                <el-select disabled v-model="form.xuezhi" />
                            </el-form-item>
                        </el-col>
                        <el-col :span="12">
                            <el-form-item label="毕业时间">
                                <el-date-picker
                                    disabled
                                    v-model="form.graduationDate"
                                    type="date"
                                    value-format="yyyy-MM-dd"
                                >
                                </el-date-picker>
                            </el-form-item>
                        </el-col>

                        <el-col :span="12">
                            <el-form-item label="取得时间">
                                <el-date-picker
                                    disabled
                                    v-model="form.getDate"
                                    type="date"
                                    value-format="yyyy-MM-dd"
                                >
                                </el-date-picker>
                            </el-form-item>
                        </el-col>
                        <el-col :span="24">
                            <el-form-item label="描述" prop="desc">
                                <el-input
                                    v-if="form.desc"
                                    disabled
                                    v-model="form.desc"
                                    type="textarea"
                                    :rows="5"
                                    placeholder="请输入描述"
                                />
                                <span v-else>无</span>
                            </el-form-item>
                        </el-col>
                        <el-col :span="24">
                            <el-form-item label="证书文件">
                                <el-link
                                    v-show="
                                        form.urlObj && form.urlObj.length > 0
                                    "
                                    type="primary"
                                    style="margin-left: 5px"
                                    @click="openImage(item.url, item.name)"
                                    v-for="item in form.urlObj"
                                    >{{ item.name }};</el-link
                                >
                                <span
                                    v-show="
                                        (!form.urlObj ||
                                            form.urlObj.length <= 0) &&
                                        $store.getters.allDefectObj[
                                            'HR-WEB-169'
                                        ] == 1
                                    "
                                    >无</span
                                >
                            </el-form-item>
                        </el-col>
                    </el-row>
                </el-form>
            </TableCard>
            <div slot="footer" class="dialog-footer">
                <el-button @click="cancelXl">返 回</el-button>
            </div>
        </el-dialog>

        <!-- 查看学位证 -->
        <el-dialog
            :title="title"
            :visible.sync="showXw"
            width="1200px"
            append-to-body
        >
            <TableCard>
                <el-form
                    ref="form"
                    :model="form"
                    :rules="rules"
                    label-width="120px"
                >
                    <el-row>
                        <el-col :xs="24" :sm="24" :md="24" :lg="24">
                            <el-divider content-position="left"
                                >员工信息</el-divider
                            >
                        </el-col>
                        <el-col :span="12">
                            <el-form-item label="姓名">
                                <el-input disabled v-model="form.realName" />
                            </el-form-item>
                        </el-col>
                        <el-col :span="12">
                            <el-form-item label="工号">
                                <el-input disabled v-model="form.staffNum" />
                            </el-form-item>
                        </el-col>

                        <el-col :span="12">
                            <!-- 部门 -->

                            <el-form-item label="部门">
                                <treeselect
                                    v-model="form.deptId"
                                    :options="deptOptions"
                                    :show-count="true"
                                    disabled
                                />
                            </el-form-item>
                        </el-col>
                        <el-col :span="12">
                            <!-- 岗位 -->
                            <el-form-item label="岗位">
                                <el-select disabled v-model="form.postId">
                                    <el-option
                                        v-for="item in postList"
                                        :key="item.postId"
                                        :label="item.postName"
                                        :value="item.postId"
                                    >
                                    </el-option>
                                </el-select>
                            </el-form-item>
                        </el-col>
                    </el-row>
                    <el-row>
                        <el-col :xs="24" :sm="24" :md="24" :lg="24">
                            <el-divider content-position="left"
                                >学历证信息</el-divider
                            >
                        </el-col>

                        <el-col :span="12">
                            <el-form-item label="证书名称">
                                <el-input
                                    disabled
                                    v-model="form.certificateName"
                                />
                            </el-form-item>
                        </el-col>

                        <el-col :span="12">
                            <el-form-item label="专业" prop="major">
                                <el-input disabled v-model="form.major" />
                            </el-form-item>
                        </el-col>
                        <el-col :span="12">
                            <el-form-item label="学位名称">
                                <el-input
                                    disabled
                                    v-model="form.degree"
                                    placeholder="请输入学位名称"
                                />
                            </el-form-item>
                        </el-col>

                        <el-col :span="12">
                            <el-form-item label="授予单位" prop="fromDw">
                                <el-input
                                    disabled
                                    v-model="form.fromDw"
                                    placeholder="请输入授予单位"
                                /> </el-form-item
                        ></el-col>

                        <el-col :span="12">
                            <el-form-item label="取得时间">
                                <el-date-picker
                                    disabled
                                    v-model="form.getDate"
                                    type="date"
                                    value-format="yyyy-MM-dd"
                                >
                                </el-date-picker>
                            </el-form-item>
                        </el-col>
                        <el-col :span="24">
                            <el-form-item label="描述" prop="desc">
                                <el-input
                                    v-if="form.desc"
                                    disabled
                                    v-model="form.desc"
                                    type="textarea"
                                    :rows="5"
                                    placeholder="请输入描述"
                                />
                                <span v-else>无</span>
                            </el-form-item>
                        </el-col>
                        <el-col :span="24">
                            <el-form-item label="证书文件">
                                <el-link
                                    v-show="
                                        form.urlObj && form.urlObj.length > 0
                                    "
                                    type="primary"
                                    style="margin-left: 5px"
                                    @click="openImage(item.url, item.name)"
                                    v-for="item in form.urlObj"
                                    >{{ item.name }};</el-link
                                >
                                <span
                                    v-show="
                                        (!form.urlObj ||
                                            form.urlObj.length <= 0) &&
                                        $store.getters.allDefectObj[
                                            'HR-WEB-169'
                                        ] == 1
                                    "
                                    >无</span
                                >
                            </el-form-item>
                        </el-col>
                    </el-row>
                </el-form>
            </TableCard>
            <div slot="footer" class="dialog-footer">
                <el-button @click="cancelXw">返 回</el-button>
            </div>
        </el-dialog>

        <!-- 证书管理页面 -->
        <el-dialog
            :title="title"
            :visible.sync="showZs"
            width="1200px"
            append-to-body
        >
            <TableCard>
                <el-form
                    ref="form"
                    :model="form"
                    :rules="rules"
                    label-width="120px"
                >
                    <el-row>
                        <el-col :span="8">
                            <el-form-item label="姓名">
                                <el-input disabled v-model="form.realName" />
                            </el-form-item>
                        </el-col>
                        <el-col :span="8">
                            <el-form-item label="工号">
                                <el-input disabled v-model="form.staffNum" />
                            </el-form-item>
                        </el-col>
                        <el-col :span="8">
                            <el-form-item label="证书类别">
                                <el-input
                                    disabled
                                    v-model="form.certificateTypeName"
                                />
                            </el-form-item>
                        </el-col>
                    </el-row>
                    <el-row>
                        <el-col :xs="24" :sm="24" :md="24" :lg="24">
                            <el-divider content-position="left"
                                >已上传图片：</el-divider
                            >
                        </el-col>

                        <el-col :span="8" v-for="item in form.urlObj">
                            <div
                                style="
                                    gap: 5px;
                                    display: flex;
                                    flex-direction: column;
                                    align-items: center;
                                    border: 1px solid #e6e6e6;
                                    padding: 5px;
                                    height: 300px;
                                "
                            >
                                <el-image
                                    style="width: 100%; height: auto"
                                    :src="baseUrl + item.url"
                                ></el-image>
                                <div>{{ item.name }}</div>
                                <div><el-button>删除</el-button></div>
                            </div>
                        </el-col>
                        <el-col :span="8">
                            <div
                                style="
                                    gap: 5px;
                                    display: flex;
                                    flex-direction: column;
                                    align-items: center;
                                    border: 1px solid #e6e6e6;
                                    padding: 5px;
                                    height: 300px;
                                    justify-content: center;
                                    margin-left: 10px;
                                "
                            >
                                <div><el-button>添加</el-button></div>
                            </div>
                        </el-col>
                    </el-row>
                </el-form>
            </TableCard>
            <div slot="footer" class="dialog-footer">
                <el-button @click="cancelXw">返 回</el-button>
            </div>
        </el-dialog>

        <!-- 证书展示弹窗 -->
        <el-dialog :visible.sync="showImage" width="800px" append-to-body>
            <div
                style="
                    display: flex;
                    flex-direction: column;
                    align-items: center;
                "
            >
                <el-image
                    style="width: 100%; height: auto"
                    :src="baseUrl + imgUrl"
                ></el-image>
                <div>{{ imgName }}</div>
            </div>
            <div slot="footer" class="dialog-footer">
                <el-button @click="cancelShowImage">关 闭</el-button>
            </div>
        </el-dialog>

        <userTableDialog
            title="用户"
            :open.sync="showTableVision"
            @confirm="setFormBase"
        >
        </userTableDialog>
    </div>
</template>

<script>
import {
    getCertificate,
    delCertificate,
    addCertificate,
    updateCertificate,
} from "@/api/back/certificate";

import { listCertificate } from "@/api/person";
import { treeselect } from "@/api/system/dept";
import Treeselect from "@riophae/vue-treeselect";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";
import { listPostAll } from "@/api/system/post";

export default {
    name: "Certificate",
    dicts: ["hrm_staff_type", "zs_type"],
    data() {
        return {
            // 职位选项
            postList: [],
            // 部门树选项
            deptOptions: [],
            deptOptionsArray: null,
            // 遮罩层
            loading: true,
            // 选中数组
            ids: [],
            // 非单个禁用
            single: true,
            // 非多个禁用
            multiple: true,
            // 显示查询条件
            showSearch: true,
            // 总条数
            total: 0,
            // 劳动合同表格数据
            certificateList: [],
            // 弹出层标题
            title: "",
            // 是否显示弹出层
            open: false,
            // 查询参数
            queryParams: {
                pageNum: 1,
                pageSize: 10,
                // orderByColumn: "createTime",
                timeArr:null, 

                code: null,
                realName: null,
                staffNum: null,
                userId: null,
                deptId: null,
                deptName: null,
                postId: null,
                postName: null,
                certificateName: null,
                certificateType: null,
                certificateUrls: null,
                getDate: null,
                desc: null,
                certificateCode: null,
                school: null,
                education: null,
                major: null,
                xuezhi: null,
                graduationDate: null,
                degree: null,
                fromDw: null,
            },
            // 表单参数
            form: {},
            // 表单校验
            rules: {
                code: [
                    {
                        required: true,
                        message: "证书编号不能为空",
                        trigger: "blur",
                    },
                ],
                realName: [
                    {
                        required: true,
                        message: "员工不能为空",
                        trigger: "change",
                    },
                ],
                staffNum: [
                    {
                        required: true,
                        message: "员工编号不能为空",
                        trigger: "blur",
                    },
                ],
                userId: [
                    {
                        required: true,
                        message: "员工id不能为空",
                        trigger: "blur",
                    },
                ],
                deptId: [
                    {
                        required: true,
                        message: "所属部门id不能为空",
                        trigger: "blur",
                    },
                ],
                deptName: [
                    {
                        required: true,
                        message: "所属部门不能为空",
                        trigger: "blur",
                    },
                ],
                postId: [
                    {
                        required: true,
                        message: "岗位ID不能为空",
                        trigger: "blur",
                    },
                ],
                postName: [
                    {
                        required: true,
                        message: "岗位名称不能为空",
                        trigger: "blur",
                    },
                ],
                certificateName: [
                    {
                        required: true,
                        message: "证书名称不能为空",
                        trigger: "blur",
                    },
                ],
                certificateType: [
                    {
                        required: true,
                        message: "证书类型不能为空",
                        trigger: "change",
                    },
                ],
                certificateUrls: [
                    {
                        required: true,
                        message: "证书url列表不能为空",
                        trigger: "blur",
                    },
                ],
                getDate: [
                    {
                        required: true,
                        message: "取得时间不能为空",
                        trigger: "blur",
                    },
                ],
            },
            showTableVision: false,
            // 查看学历证
            showXl: false,
            showXw: false,
            showZs: false,

            //证书弹窗信息
            showImage: false,
            imgName: "",
            imgUrl: "",

            //文件访问的基础地址
            // 文件下载链接
            baseUrl: process.env.VUE_APP_BASE_API,
        };
    },
    components: {
        // 导入时间控件
        Treeselect,
    },

    created() {
        this.getList();
        this.getTreeselect();
        this.getPostList();
    },
    methods: {
        //打开学位展示弹窗
        openXw(url, name) {
            this.imgUrl = url;
            this.imgName = name;
            this.showXw = true;
        },
        //关闭学位弹窗
        cancelXw() {
            this.showXw = false;
        },

        //打开证书展示弹窗
        openImage(url, name) {
            if (this.$store.getters.allDefectObj["HR-WEB-170"] == 0) {
                return;
            }

            this.imgUrl = url;
            this.imgName = name;
            this.showImage = true;
        },
        //关闭证书展示弹窗
        cancelShowImage() {
            this.showImage = false;
            this.imgName = "";
            this.imgUrl = "";
        },

        //关闭学历弹窗
        cancelXl() {
            this.showXl = false;
        },
        //添加学位
        handleAddXw() {
            this.reset();
            this.open = true;
            this.title = "添加学位证书";
            this.form.certificateType = 2;
        },

        parsePostName(ids) {
            if (!ids) {
                return "—";
            }
            if (ids != null && ids != "") {
                let obj = this.postList.find((item) => item.postId == ids);
                if (obj) {
                    return obj.postName;
                }
            }
            return "—";
        },
        getPostList() {
            listPostAll().then((res) => {
                this.postList = res.data;
            });
        },

        parseSelectTree(id) {
            console.log(this.deptOptionsArray, "this.deptOptionsArray");
            let tempObj = this.deptOptionsArray.find((item) => item.id == id);
            if (tempObj) {
                return tempObj.label;
            }
            return "—";
        },

        /** 查询部门下拉树结构 */
        getTreeselect() {
            treeselect().then((response) => {
                this.deptOptions = response.data;
                this.deptOptionsArray = this.flattenArray(this.deptOptions);
            });
        },
        flattenArray(arr) {
            let result = [];
            for (let i = 0; i < arr.length; i++) {
                let item = arr[i];
                result.push(item);
                if (item.children) {
                    result = result.concat(this.flattenArray(item.children));
                }
            }
            return result;
        },
        parseSelectTree(id) {
            console.log(this.deptOptionsArray, "this.deptOptionsArray");
            let tempObj = this.deptOptionsArray.find((item) => item.id == id);
            if (tempObj) {
                return tempObj.label;
            }
            return "—";
        },
        //设置用户
        setFormBase(row) {
            this.form.userId = row.userId;
            this.form.realName = row.realName;

            this.form.staffNum = row.staffNum;
            this.form.deptId = row.deptId;
            //按，号分割
            let tempPostId = row.postId ? row.postId.split(",") : [];
            if (tempPostId.length > 0) {
                this.form.postId = Number(tempPostId[0]);
            }

            // 拿到name

            this.form.deptName = this.parseSelectTree(this.form.deptId);
            this.form.postName = this.parsePostName(this.form.postId);
        },

        openSecelctOneUser() {
            this.showTableVision = true;
        },

        /** 查询劳动合同列表 */
        getList() {
            this.loading = true;
            if (
                this.queryParams.timeArr &&
                this.queryParams.timeArr.length > 0
            ) {
                this.queryParams.startDate = this.queryParams.timeArr[0];
                this.queryParams.endDate = this.queryParams.timeArr[1];
            } else {
                this.queryParams.startDate = null;
                this.queryParams.endDate = null;
            }
            listCertificate(this.queryParams).then((response) => {
                this.certificateList = response.rows;
                this.total = response.total;
                this.loading = false;
            });
        },
        // 取消按钮
        cancel() {
            this.open = false;
            this.reset();
        },
        // 表单重置
        reset() {
            this.form = {
                id: null,
                code: null,
                realName: null,
                staffNum: null,
                userId: null,
                deptId: null,
                deptName: null,
                postId: null,
                postName: null,
                certificateName: null,
                certificateType: null,
                certificateUrls: null,
                getDate: null,
                desc: null,
                remark: null,
                createTime: null,
                createBy: null,
                updateBy: null,
                updateTime: null,
                certificateCode: null,
                school: null,
                education: null,
                major: null,
                xuezhi: null,
                graduationDate: null,
                degree: null,
                fromDw: null,
            };
            this.resetForm("form");
        },
        /** 查询按钮操作 */
        handleQuery() {
            this.queryParams.pageNum = 1;
            this.getList();
        },
        /** 重置按钮操作 */
        resetQuery() {
            this.resetForm("queryForm");
            this.queryParams.timeArr = [];
            this.handleQuery();
        },
        // 多选框选中数据
        handleSelectionChange(selection) {
            this.ids = selection.map((item) => item.id);
            this.single = selection.length !== 1;
            this.multiple = !selection.length;
        },
        /** 新增按钮操作 */
        handleAdd() {
            this.reset();
            this.open = true;
            this.title = "添加劳动合同";
        },
        /** 新增按钮操作 */
        handleAddXl() {
            this.reset();
            this.open = true;
            this.title = "添加学历证书";
            this.form.certificateType = 1;
        },

        //查看学位
        viewXw(row) {
            this.reset();
            const id = row.id || this.ids;
            getCertificate(id).then((response) => {
                this.form = response.data;
                // 存在证书文件
                if (this.form.certificateUrls) {
                    let tempList = this.form.certificateUrls.split(",");
                    this.form.urlObj = tempList.map((item) => {
                        let fileName = item.substr(item.lastIndexOf("/") + 1);
                        let pureFileName = fileName.split("_")[0];

                        return {
                            url: item,
                            name: pureFileName,
                        };
                    });
                }
                this.showXw = true;
                this.title = "查看学位证书信息";
            });
        },
        showZswj(row) {
            this.reset();
            const id = row.id || this.ids;
            getCertificate(id).then((response) => {
                this.form = response.data;
                // 存在证书文件
                if (this.form.certificateUrls) {
                    let tempList = this.form.certificateUrls.split(",");
                    this.form.urlObj = tempList.map((item) => {
                        let fileName = item.substr(item.lastIndexOf("/") + 1);
                        let pureFileName = fileName.split("_")[0];

                        return {
                            url: item,
                            name: pureFileName,
                        };
                    });
                }
                this.showZs = true;
                this.title = "证书文件";
            });
        },

        //查看学历
        viewXl(row) {
            this.reset();
            const id = row.id || this.ids;
            getCertificate(id).then((response) => {
                this.form = response.data;
                // 存在证书文件
                if (this.form.certificateUrls) {
                    let tempList = this.form.certificateUrls.split(",");
                    this.form.urlObj = tempList.map((item) => {
                        let fileName = item.substr(item.lastIndexOf("/") + 1);
                        let pureFileName = fileName.split("_")[0];

                        return {
                            url: item,
                            name: pureFileName,
                        };
                    });
                }
                this.showXl = true;
                this.title =
                    this.$store.getters.allDefectObj["HR-WEB-168"] == 0
                        ? "查看学位证书信息"
                        : "查看学历证书信息";
            });
        },

        /** 修改按钮操作 */
        handleUpdate(row) {
            this.reset();
            const id = row.id || this.ids;
            getCertificate(id).then((response) => {
                this.form = response.data;
                this.open = true;
                this.title = "修改学历证书";
            });
        },
        /** 提交按钮 */
        submitForm() {
            this.$refs["form"].validate((valid) => {
                if (valid) {
                    if (this.form.id != null) {
                        updateCertificate(this.form).then((response) => {
                            this.$modal.msgSuccess("修改成功");
                            this.open = false;
                            this.getList();
                        });
                    } else {
                        addCertificate(this.form).then((response) => {
                            this.$modal.msgSuccess("新增成功");
                            this.open = false;
                            this.getList();
                        });
                    }
                }
            });
        },
        /** 删除按钮操作 */
        handleDelete(row) {
            const ids = row.id || this.ids;
            this.$modal
                .confirm('是否确认删除劳动合同编号为"' + ids + '"的数据项？')
                .then(function () {
                    return delCertificate(ids);
                })
                .then(() => {
                    this.getList();
                    this.$modal.msgSuccess("删除成功");
                })
                .catch(() => {});
        },
        // /** 导出按钮操作 */
        // handleExport() {
        //     this.download(
        //         "back/certificate/export",
        //         {
        //             ...this.queryParams,
        //         },
        //         `certificate_${new Date().getTime()}.xlsx`
        //     );
        // },

        /** 导出按钮操作 */
        handleExport() {
            const date = new Date();
            const year = date.getFullYear();
            const month = String(date.getMonth() + 1).padStart(2, "0");
            const day = String(date.getDate()).padStart(2, "0");
            this.download(
                "back/certificate/export",
                {
                    ...this.queryParams,
                },
                `员工证书信息_${year}年${month}月${day}日.xlsx`
            );
        },
    },
};
</script>
